Skip to content

fix(sahi): Fix Polygon Repair and Empty Polygon Issues #1118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 32 commits into
base: main
Choose a base branch
from

Conversation

SunHao-AI
Copy link

  • Added repair_polygon and repair_multipolygon functions to repair invalid polygons and multipolygons.
  • Implemented coco_segmentation_to_shapely function to convert COCO format segmentation data into Shapely objects.
  • Enhanced the get_union_polygon function to handle empty polygons using the newly implemented conversion and repair methods.

SunHao-AI and others added 4 commits January 10, 2025 10:08
- Added `repair_polygon` and `repair_multipolygon` functions to repair invalid polygons and multipolygons.
- Implemented `coco_segmentation_to_shapely` function to convert COCO format segmentation data into Shapely objects.
- Enhanced the `get_union_polygon` function to handle empty polygons using the newly implemented conversion and repair methods.
- 在 slice_image 和 SliceDataset 中添加 inference_org_image 参数- 如果 inference_org_image 为 True,将原图添加到裁剪列表中
- 更新函数文档和注释以支持新功能
@fcakyon fcakyon requested a review from Copilot March 5, 2025 01:21
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Overview

This PR fixes issues related to invalid and empty polygons in SAHI by adding repair functions and converting COCO segmentation data into valid Shapely objects. It also enhances the union logic in mask processing and adds an inference image flag to the slicing functions.

  • Added repair_polygon, repair_multipolygon, and coco_segmentation_to_shapely functions for polygon corrections.
  • Updated get_merged_mask to handle empty polygons using the new conversion methods.
  • Introduced an inference_org_image parameter to slicing functions to optionally include the original image in the output.

Reviewed Changes

File Description
sahi/postprocess/utils.py Added functions to repair invalid polygons and update union mask logic.
sahi/slicing.py Added and propagated the inference_org_image parameter in slicing logic.

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (2)

sahi/slicing.py:89

  • [nitpick] Consider using consistent naming for coordinate variables (e.g., use 'x_max' instead of 'xmax') to maintain clarity across the code.
xmax = min(image_width, x_max)

sahi/postprocess/utils.py:245

  • [nitpick] Consider removing or simplifying decorative comment markers to maintain a clean and professional code style.
###################################################sunhao###################################################

@@ -64,6 +65,81 @@ def tolist(self):
else:
return self.list

###################################################sunhao###################################################
Copy link
Preview

Copilot AI Mar 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider removing or simplifying decorative comment markers to maintain a clean and professional code style.

Suggested change
###################################################sunhao###################################################
# Polygon repair functions

Copilot uses AI. Check for mistakes.

@fcakyon
Copy link
Collaborator

fcakyon commented Mar 5, 2025

@SunHao-AI can you please fix the failing tests 🤗

@mario-dg
Copy link
Contributor

@fcakyon, I need this fix, as it probably also addresses #1094 but with a cleaner implementation. Should I open a new PR, implement the changes and make sure the tests are running?

@SunHao-AI
Copy link
Author

您能否修复失败的测试 🤗

def coco_segmentation_to_shapely(
    segmentation: Union[List, List[List]]
):
    """
    Fix segment data in COCO format

    :param segmentation: segment data in COCO format
    :return:
    """
    if isinstance(segmentation, List) and all([not isinstance(seg, List) for seg in segmentation]):
        segmentation = [segmentation]
    elif isinstance(segmentation, List) and all([isinstance(seg, List) for seg in segmentation]):
        pass
    else:
        raise ValueError("segmentation must be list or list[list]")

    polygon_list = []

    for coco_polygon in segmentation:
        point_list = list(zip(coco_polygon[::2], coco_polygon[1::2]))
        shapely_polygon = Polygon(point_list)
        polygon_list.append(repair_polygon(shapely_polygon))

    shapely_multipolygon = repair_multipolygon(MultiPolygon(polygon_list))
    return shapely_multipolygon

@SunHao-AI
Copy link
Author

,我需要这个修复程序,因为它可能也解决了 #1094,但实现了更简洁。我应该打开一个新的 PR,实施更改并确保测试正在运行吗?

ok

@mario-dg
Copy link
Contributor

@SunHao-AI, if you are already at it, you might just make the fix yourself, as you already posted the solution 😄

fcakyon and others added 5 commits March 31, 2025 09:57
- 从 requirements.txt 中删除了 opencv-python 包
- 此更改可能是为了简化项目依赖或避免特定版本冲突
# Conflicts:
#	requirements.txt
github-merge-queue bot pushed a commit that referenced this pull request May 5, 2025
fcakyon and others added 7 commits May 25, 2025 00:01
- 将原始的 isinstance检查从 'list' 更改为 'List',以符合类型注解的最佳实践
- 保持了原有的逻辑结构,没有引入新的功能或改变现有行为
- 为 image_predictions.masks 添加默认值,避免空指针异常
- 优化掩码 resizing 逻辑,使用原始图像尺寸- 重构部分代码,提高可读性和维护性
- 添加了 get_letter_args 函数,用于计算 resize 和 pad 尺寸
- 优化了对原始预测结果的处理逻辑
- 在将 bool_mask 转换为 COCO格式分割结果时,添加了缺失的步骤
- 先将 bool_mask 缩放回原始尺寸,然后使用 get_coco_segmentation_from_bool_mask 函数进行转换- 修复了 previously 逻辑中未完成的转换过程
- 优化了掩码预测的缩放逻辑,提高了准确性
- 移除了不必要的条件判断和打印语句,简化了代码
- 增加了对 full_shape_list[-1] 的
- 修改了对原始图像尺寸的检查方式,以确保正确的尺寸比较
- 优化了代码结构,提高了可读性和性能
SunHao-AI and others added 9 commits June 9, 2025 11:35
- 移除了对 letter_box 的逆操作,简化了掩码缩放流程
- 统一使用 cv2.resize 对掩码进行缩放,提高了代码可读性
- 删除了不必要的条件判断,减少了代码复杂度
- 删除了用于逆 letter_box 的注释代码
- 保留了原始逻辑,即直接调整 bool_mask 大小以匹配原始图像尺寸
- 新增代码以处理原图推理时的 letterbox 逆操作- 修复了在 sahi 新版本中可能导致推理失败的问题
- 优化了图像尺寸计算逻辑, 提高代码可读性
- 在 Ultralytics 模型中,修复了使用原图进行推理时出现的 letterbox 问题
- 调整了 bool_mask 的处理逻辑,确保在不同形状的图像上进行推理时能够正确处理边界框和掩码
-重构了 sahi/predict.py 中的预测流程,使用原始模型进行推理
- 修复了 sahi/models/ultralytics.py 中的 mask 处理问题
-移除了不必要的注释和冗余代码
- 优化了 letterbox 处理逻辑
-将 image_predictions.data 改为 image_predictions.boxes.data 以避免属性错误
- 此修改确保在没有掩码预测的情况下,程序不会抛出属性错误异常
@gboeer
Copy link
Contributor

gboeer commented Jul 1, 2025

The first checks fail because of the type annotation, i.e. tuple is not available in older python versions as a type annotation.
You can either use Tuple from the typing package as has been done in the rest of the codebase, or have to implement fallbacks for older python versions.

fcakyon and others added 5 commits July 1, 2025 13:18
- 调整 POSTPROCESS_NAME_TO_CLASS 字典格式,提高可读性
-移除未使用的 logger 对象- 修复对象预测列表推导式中的缩进问题
- 在模型推理时添加 verbose 参数控制输出
- 优化视频帧或图像迭代的代码结构
- 添加对无效 CUDA 设备索引的处理
- 改进代码注释,增加中文注释以提高可读性
- 保留原有函数功能的同时,提升了代码质量和健壮性
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants